package com.tengxun.api.yuyin;

import com.tencentcloudapi.asr.v20190614.AsrClient;
import com.tencentcloudapi.asr.v20190614.models.*;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;

public class AsrTaskStatusExample {
    private static AsrClient client;
    
    static {
        try {
            // 初始化AsrClient
            Credential cred = new Credential("AKIDfDYfau0rXzDDyZ9czKivwleJkxzAFdtw", "vgFZfXBamkU2ZtzwsfO3bx5HMGUbYKsC");
            
            HttpProfile httpProfile = new HttpProfile();
            httpProfile.setEndpoint("asr.tencentcloudapi.com");
            
            ClientProfile clientProfile = new ClientProfile();
            clientProfile.setHttpProfile(httpProfile);
            
            client = new AsrClient(cred, "ap-beijing", clientProfile);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    public static void main(String[] args) {
        try {
            // 假设你已经有了一个任务ID (从CreateRecTask返回的结果中获取)
            Long taskId = 12767437329L; // 替换为实际的TaskId
            
            // 轮询获取任务状态
            pollTaskStatus(taskId);
            
        } catch (TencentCloudSDKException e) {
            System.out.println("API调用错误: " + e.toString());
        } catch (InterruptedException e) {
            System.out.println("线程中断: " + e.getMessage());
        }
    }
    
    /**
     * 轮询任务状态直到任务完成或失败
     * @param taskId 任务ID
     * @throws TencentCloudSDKException
     * @throws InterruptedException
     */
    public static void pollTaskStatus(Long taskId) throws TencentCloudSDKException, InterruptedException {
        int maxRetries = 30; // 最大轮询次数
        int retryInterval = 5000; // 轮询间隔(毫秒)
        
        for (int i = 0; i < maxRetries; i++) {
            DescribeTaskStatusRequest req = new DescribeTaskStatusRequest();
            req.setTaskId(taskId);
            
            DescribeTaskStatusResponse resp = client.DescribeTaskStatus(req);
            
            TaskStatus data = resp.getData();
            Long status = data.getStatus();
            String statusStr = getStatusString(status);
            
            System.out.println("轮询第" + (i+1) + "次:");
            System.out.println("任务ID: " + data.getTaskId());
            System.out.println("任务状态: " + status + " (" + statusStr + ")");
            
            // 根据状态处理
            switch (status.intValue()) {
                case 0: // 任务等待中
                    System.out.println("任务正在等待中...");
                    break;
                case 1: // 任务执行中
                    System.out.println("任务正在执行中...");
                    break;
                case 2: // 任务成功
                    System.out.println("任务执行成功!");
                    printTaskResult(data);
                    return;
                case 3: // 任务失败
                    System.out.println("任务执行失败!");
                    System.out.println("错误码: " + data.getErrorMsg());
                    return;
                case 4: // 任务过期
                    System.out.println("任务已过期!");
                    return;
                default:
                    System.out.println("未知状态: " + status);
                    break;
            }
            
            // 等待一段时间后继续轮询
            if (i < maxRetries - 1) {
                System.out.println("等待 " + (retryInterval/1000) + " 秒后继续轮询...\n");
                Thread.sleep(retryInterval);
            }
        }
        
        System.out.println("轮询超时，任务未在预期时间内完成。");
    }
    
    /**
     * 打印任务结果
     * @param data 任务状态数据
     */
    private static void printTaskResult(TaskStatus data) {
        System.out.println("=== 识别结果 ===");
        System.out.println("结果类型: " + data.getStatus());
        System.out.println("结果详情: " + data.getResult());
        
        // 如果有详细的识别结果
        if (data.getResult() != null && data.getResult().length() > 0) {
            System.out.println("详细结果:"+data.getResult());

        }
    }
    
    /**
     * 将状态码转换为可读字符串
     * @param status 状态码
     * @return 状态描述
     */
    private static String getStatusString(Long status) {
        switch (status.intValue()) {
            case 0: return "等待中";
            case 1: return "执行中";
            case 2: return "成功";
            case 3: return "失败";
            case 4: return "过期";
            default: return "未知";
        }
    }
}
